package com.xauto.pioneer.appuser.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.model.CarReceiptableInfo;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * 申请用车发票适配器
 * Desc:订单列表 适配器
 */
public class ApplyCarReceiptAdapter extends BaseAdapter {
    private Context context;
    private List<HashMap<String, Object>> list;
    private LayoutInflater layoutInflater;


    private TextView tv_applycarreceipt_no;
    private TextView tv_applycarreceipt_state;
    private TextView tv_applycarreceipt_time;
    private TextView tv_applycarreceipt_price;
    private CheckBox cb_applycarreceipt;


    public ApplyCarReceiptAdapter(Activity context, List<HashMap<String, Object>> list) {
        this.context = context;
        this.list = list;//list中checkbox状态为false
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int index = position;
        ViewCache viewCache = new ViewCache();
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_applycarreceipt, null);
            tv_applycarreceipt_no = (TextView) convertView.findViewById(R.id.tv_applycarreceipt_no);
            tv_applycarreceipt_state = (TextView) convertView.findViewById(R.id.tv_applycarreceipt_state);
            tv_applycarreceipt_time = (TextView) convertView.findViewById(R.id.tv_applycarreceipt_time);
            tv_applycarreceipt_price = (TextView) convertView.findViewById(R.id.tv_applycarreceipt_price);
            cb_applycarreceipt = (CheckBox) convertView.findViewById(R.id.cb_applycarreceipt);

            viewCache.tv_applycarreceipt_no = tv_applycarreceipt_no;
            viewCache.tv_applycarreceipt_state = tv_applycarreceipt_state;
            viewCache.tv_applycarreceipt_time = tv_applycarreceipt_time;
            viewCache.tv_applycarreceipt_price = tv_applycarreceipt_price;
            viewCache.cb_applycarreceipt = cb_applycarreceipt;
            convertView.setTag(viewCache);
        } else {
            viewCache = (ViewCache) convertView.getTag();
            tv_applycarreceipt_no = viewCache.tv_applycarreceipt_no;
            tv_applycarreceipt_state = viewCache.tv_applycarreceipt_state;
            tv_applycarreceipt_time = viewCache.tv_applycarreceipt_time;
            tv_applycarreceipt_price = viewCache.tv_applycarreceipt_price;
            cb_applycarreceipt = viewCache.cb_applycarreceipt;
        }
        try {
            JSONObject js = new JSONObject(list.get(position).get("obj").toString());
            CarReceiptableInfo info = new CarReceiptableInfo().parse(js);
//            Log.e("tag",info.toString());context.getString(R.string.order_no,orderInfo.car_order_no)
            tv_applycarreceipt_no.setText(context.getString(R.string.order_no, info.car_order_no));
            tv_applycarreceipt_state.setText("已支付");
            tv_applycarreceipt_time.setText(context.getString(R.string.order_date, info.order_time_format));
            tv_applycarreceipt_price.setText(context.getString(R.string.order_account, info.total_amount_format));
        } catch (Exception e) {
            e.printStackTrace();
        }
//        viewCache.cb_applycarreceipt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    list.get(index).put("boolean",true);
//                } else {
//                    list.get(index).put("boolean",false);
//                }
//            }
//        });
        if ((Boolean)list.get(position).get("boolean")) {
            viewCache.cb_applycarreceipt.setChecked(true);
        } else {
            viewCache.cb_applycarreceipt.setChecked(false);
        }
        return convertView;
    }
    public class ViewCache {
        TextView tv_applycarreceipt_no;
        TextView tv_applycarreceipt_state;
        TextView tv_applycarreceipt_time;
        TextView tv_applycarreceipt_price;
        public CheckBox cb_applycarreceipt;
    }

}
